package com.webdrider.model.validators;

import com.common.model.abstracts.AbstractRequest;
import com.common.model.abstracts.AbstractValidator;
import com.common.security.abstracts.AbstractError;
import com.webdrider.model.action.snippet.Domain;
import com.webdrider.model.requests.snippet.AddSnippetDomainRequest;
import com.webdrider.security.errors.SnippetError;

public class AddSnippetDomainValidator extends AbstractValidator {

	public static final int DOMAIN_MIN_LENGTH = 1;
	public static final int DOMAIN_MAX_LENGTH = 250;
	
	@Override
	public boolean validate(AbstractRequest abstractRequest) {
		AddSnippetDomainRequest request = null;
		
		if (abstractRequest instanceof AddSnippetDomainRequest) {
			request = (AddSnippetDomainRequest) abstractRequest;
		} else {
			error = new SnippetError(AbstractError.INVALID_REQUEST);
			return false;
		}
		
		Domain domain = request.getDomain();
		
		if (domain.getValue() == null || domain.getValue().length() == 0) {
			error = new SnippetError(SnippetError.NO_DOMAIN_PROVIDED);
			return false;
		}
		
		if (domain.getValue().length() < DOMAIN_MIN_LENGTH ||
				domain.getValue().length() > DOMAIN_MAX_LENGTH) {
			error = new SnippetError(SnippetError.DOMAIN_BAD_FORMAT);
			return false;
		}
		
		return true;
	}

}
